﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Linq;

namespace SchemeManager
{
    /// <summary>
    /// SystemConfig.xaml 的交互逻辑
    /// </summary>
    public partial class SystemConfig : Window
    {
        public SystemConfig()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SystemConfig_Loaded);
        }

        void SystemConfig_Loaded(object sender, RoutedEventArgs e)
        {
            string configFilepath = this.GetType().Assembly.Location + ".config";
            try
            {

                XDocument xDoc = XDocument.Load(configFilepath);
                this.textBox1.Text = xDoc.Descendants("endpoint").Where((xe) => xe.Attribute("binding").Value == "netTcpBinding").First().Attribute("address").Value;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string configFilepath = this.GetType().Assembly.Location + ".config";

            try
            {
                XDocument xDoc = XDocument.Load(configFilepath);

                xDoc.Descendants("endpoint").Where((xe) => xe.Attribute("binding").Value == "netTcpBinding").First().Attribute("address").Value = textBox1.Text;

                xDoc.Save(configFilepath);

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
